<?php
class User extends AppModel {

	var $name = 'User';
	var $validate = array(
		'role_id' => array('numeric'),
		'username' => array(
      'alphanumeric' => array(
        'rule' => 'alphanumeric',
        'required' => true, 
        'allowEmpty' => false,
        'message' => 'Only alphabets and numbers allowed',
        'last' => true
      ),
      'length' => array(
        'rule' => array('between', 4, 20),
        'on' => 'create',
        'message' => 'Your username must be between 4 and 20 characters long',
        'last' => true
      ),
      'unique' => array(
        'rule' => 'isUnique',
        'on' => 'create',
        'message' => 'This username has already been taken.'
      ),
    ),
    'password' => array(
        'rule' => array('minLength', 6), 
        'allowEmpty' => false,
        'required' => true, 
        'message' => 'Passwords do not match'
    )
    
	);
	
  function confirmPassword($data) {
    $valid = false;
    if ($data['password'] == Security::hash(Configure::read('Security.salt') . $this->data['User']['password_confirm'])) {
      $valid = true;
    }
    return $valid;
  }
  
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
    'Role' => array(
      'className' => 'Role',
      'foreignKey' => 'role_id',
      'conditions' => '',
      'fields' => '',
      'order' => ''
    )
  );
	
  var $hasOne = 'PersonalProfile';
  
  //The Associations below have been created with all possible keys, those that are not needed can be removed
  var $hasMany = array(
    'Ballot' => array(
      'className' => 'Ballot',
      'foreignKey' => 'user_id',
      'conditions' => '',
      'fields' => '',
      'order' => ''
    )
  );
  
	//tell Acl behavior (native cake) that the user will be a "requester" (aro)
	var $actsAs = array('Acl' => 'requester');
	/**
   * The id of this parent node.
	 * @return unknown_type the role_id of the user.
	 */
	function parentNode() {
		if($this->id) {
			$data = $this->read();
			if(!empty($this->data) && $data['User']['role_id'])
        return array('Role' => array('id' => $data['User']['role_id']));
		}
		return null;
	}
}
?>